package com.cskaoyan.controller.admin;

import com.cskaoyan.bean.BaseData;
import com.cskaoyan.bean.bo.CreateGoodsBO;
import com.cskaoyan.bean.bo.GoodsParam;
import com.cskaoyan.bean.pojo.MallGoods;
import com.cskaoyan.bean.vo.BaseRespVo;
import com.cskaoyan.bean.vo.CatAndBrandVO;
import com.cskaoyan.bean.vo.GoodsDetailVO;
import com.cskaoyan.service.GoodsService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * @author: harveyliu
 * @description: 商品管理的控制层代码逻辑
 * @time: 2021/7/8 20:50
 */
@RestController
@RequestMapping("admin/goods")
public class GoodsController {

    @Autowired
    GoodsService goodsService;

    /**
     * @Author harvey
     * @description: 用来查询商品列表，有分页的要求，还有可以根据编号和商品名称的模糊查询
     * @Date 2021/7/8 21:37
     * url: http://localhost:8083/admin/goods/list?page=1&limit=20&sort=add_time&order=desc
     */
    @RequestMapping("list")
    @RequiresPermissions("admin:goods:list")
    public BaseRespVo queryAllGoodsByConditions(GoodsParam param) {
        BaseData<MallGoods> mallGoodsBaseData = goodsService.queryUserByConditions(param);
        return BaseRespVo.ok(mallGoodsBaseData);
    }
    
    /**
     * @Author harvey
     * @description: 查找所有的分类和品牌的控制层
     * @Date 2021/7/8 22:14
     * url: http://localhost:8083/admin/goods/catAndBrand
     */
    @RequestMapping("catAndBrand")
    public BaseRespVo queryAllCatAndBrand() {
        CatAndBrandVO catAndBrandVO = goodsService.queryAllCategory();
        return BaseRespVo.ok(catAndBrandVO);
    }

    /**
     * @Author harvey
     * @description: 用来接收请求商品详情的映射
     * @Date 2021/7/9 8:57
     * url:
     */
    @RequestMapping("detail")
    @RequiresPermissions("admin:goods:read")
    public BaseRespVo queryGoodsDetailByGoodsId(Integer id) {
        GoodsDetailVO goodsDetailVO = goodsService.queryGoodsDetailByGoodsId(id);
        return BaseRespVo.ok(goodsDetailVO);
    }

    @RequestMapping("create")
    @RequiresPermissions("admin:goods:create")
    public BaseRespVo createGoods(@RequestBody CreateGoodsBO createGoodsBO) {
        Integer status = goodsService.insertGoods(createGoodsBO);
        if (status == 0) {
            return BaseRespVo.ok();
        } else if (status.equals(611)){
            BaseRespVo baseRespVo = BaseRespVo.fail("商品名已经存在");
            baseRespVo.setErrno(611);
            return baseRespVo;
        } else if (status.equals(1)) {
            return BaseRespVo.fail("商品名不能为空!");
        } else if (status.equals(2)) {
            return BaseRespVo.fail("商品参数表不能为空!");
        } else if (status.equals(3)) {
            return BaseRespVo.fail("商品货品表不能为空!");
        } else if (status.equals(4)) {
            return BaseRespVo.fail("商品规格表不能为空!");
        } else if (status.equals(5)) {
            return BaseRespVo.fail("商品编号不能为空!");
        }
        return BaseRespVo.fail("插入失败");
    }

    @RequestMapping("update")
    @RequiresPermissions("admin:goods:update")
    public BaseRespVo updateGoods(@RequestBody CreateGoodsBO createGoodsBO) {
        goodsService.updateGoodsById(createGoodsBO);
        return BaseRespVo.ok();
    }

    @RequestMapping("delete")
    @RequiresPermissions("admin:goods:delete")
    public BaseRespVo deleteGoods(@RequestBody MallGoods mallGoods) {
        goodsService.deleteGoodsById(mallGoods.getId());
        return BaseRespVo.ok();
    }

}
















